package cl.tieba.resource;

import static tieba.core.utils.ReturnType.fail;
import static tieba.core.utils.ReturnType.success;

import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import sign.tieba.utils.SignUtils;
import tieba.core.entity.TieBar;
import tieba.core.entity.TiebaUser;
import tieba.core.utils.TBUtils;
import tieba.core.utils.entity.Result;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;

@Path("sign")
@Api("签到")
@Produces(MediaType.APPLICATION_JSON)
public class SignResource {

	@POST
	@ApiOperation(response = Result.class, value = "提交用户签到")
	public Response sign(@ApiParam("贴吧用户对象") TiebaUser user) {
		user = TBUtils.login(user);
		if (user.getBDUSS() != null && !"".equals(user.getBDUSS().trim()))
			SignUtils.signAll(user.getBDUSS(),
					TBUtils.getClientLikes(user.getBDUSS()));
		else
			return fail();
		return success();
	}

	@GET
	@ApiOperation(response = Result.class, value = "提交Cookie签到")
	@Path("/{cookie}")
	public Response sign(
			@ApiParam("BDUSS Cookie字符串") @PathParam("cookie") String cookie) {
		List<TieBar> bs = TBUtils
				.getClientLikes(cookie);
		SignUtils.signAll(cookie, bs);
		return success();
	}

	@GET
	@ApiOperation(response = Result.class, value = "单个贴吧签到")
	@Path("/{fn}/{cookie}")
	public Response sign(@ApiParam("贴吧名称") @PathParam("fn") String fn,
			@ApiParam("BDUSS Cookie字符串") @PathParam("cookie") String cookie) {
		SignUtils.signOne(cookie, fn);
		return success();
	}
}
